<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>锋豫智慧平台</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="css/public.css" media="all">
</head>

<body>
<div class="layuimini-container">
    <div class="layuimini-main">
      <div class="layui-col-lg-offset3 layui-col-lg6">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>添加保修单</legend>
        </fieldset>
        <form class="layui-form" action="">


            <input type="hidden" name="id" id="repairId" value="">


            <div class="layui-form-item">
                <label class="layui-form-label">所属小区：</label>
                <div class="layui-input-block">
                    <select id="communityList" name="communityId" lay-verify="community" lay-event="community">
                        <option id="communityname" value="">---选择所属小区---</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">报修人员：</label>
                <div class="layui-input-block">
                    <select id="ownerList" name="ownerId" class="form-control" >
                        <option value="">---选择报修人员---</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">设备名称：</label>
                <div class="layui-input-block">
                    <select id="equipmentList" name="equipmentId" class="form-control" >
                        <option value="">---选择报修设备---</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">维修状态：</label>

                    <div class="layui-input-block">
                        <input checked="" type="radio" name="state"  value="1" title="已维修">
                        <input checked="" type="radio" name="state"  value="2" title="待处理">
                    </div>

            </div>






            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="demo1">确定添加</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
		</div>
    </div>
</div>



<script src="lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script type="text/javascript">

    layui.use(['layer', 'form', 'table', 'layedit', 'laydate', 'jquery'], function () {
        var form = layui.form,
            laydate = layui.laydate,
            layer = layui.layer,
            $ = layui.jquery;

        var s = window.decodeURIComponent(location.search.slice(1).split("=")[1]);
        var s1 = JSON.parse(s);

        if (s1 != null) {

            //页面打开时异步加载数据,查询信道
            $(function () {

                $("#repairId").val(s1.id);

                $.ajax({
                    type: "POST",
                    url: "repair/addRepair1",
                    success: function (data) {
                        $.each(data.data, function (index, item) {
                            if (item.id === s1.communityId) {

                                $("#communityList").append('<option value="'+ item.id +'" selected="selected">'+ item.name +'</option>');


                            } else {

                                $("#communityList").append(new Option(item.name, item.id));

                                // option.setAttribute("selected",'true');
                            }

                            // form.render('select');

                        });
                        layui.form.render("select");
                    }, error: function () {
                        alert("遍历communityList失败");
                    }
                });


                $.ajax({
                    type: "POST",
                    url: "repair/addRepair2",
                    success: function (data) {
                        $.each(data.data, function (index, item) {
                            if (item.id === s1.ownerId) {

                                $("#ownerList").append('<option value="'+ item.id +'" selected="selected">'+ item.name +'</option>');


                            } else {

                                $("#ownerList").append(new Option(item.name, item.id));

                                // option.setAttribute("selected",'true');
                            }
                        });
                        layui.form.render("select");
                    }, error: function () {
                        alert("遍历ownerList失败");
                    }
                });


                $.ajax({
                    type: "POST",
                    url: "repair/addRepair3",
                    success: function (data) {
                        $.each(data.data, function (index, item) {
                            if (item.id === s1.equipmentId) {

                                $("#equipmentList").append('<option value="'+ item.id +'" selected="selected">'+ item.name +'</option>');


                            } else {

                                $("#equipmentList").append(new Option(item.name, item.id));

                                // option.setAttribute("selected",'true');
                            }
                        });
                        layui.form.render("select");
                    }, error: function () {
                        alert("遍历equipmentList失败");
                    }
                });


                // // 回显
                // $.ajax({
                //
                //     type:"POST",
                //     url:"repair/updateRepair?" + "id=" +data1.id,
                //
                //     success:function(date1){
                //
                //         // $("input[name=communityId]").val([date1.community.name]);
                //         // $("input[name=ownerId]").val([date1.owner.name]);
                //         // $("input[name=equipmentId]").val([date1.equipment.name]);
                //         // $("input[name=state]").val([date1.state]);//回显
                //         layui.form.render("select");
                //     }, error: function () {
                //         alert("根据id查询失败");
                //     }
                //
                // });


                // $('[name=state]').each(function(i,item){
                //     if($(item).val()==res.state){
                //         $(item).prop('checked',true);
                //         layui.use('form',function(){
                //             var form = layui.form();
                //             form.render();
                //             alert(form);
                //         });
                //
                //     }
                // });



            });
        }
        else {
            $(function () {

                $.ajax({
                    type: "POST",
                    url: "repair/addRepair1",
                    success: function (data) {
                        $.each(data.data, function (index, item) {
                                $("#communityList").append(new Option(item.name, item.id));
                        });
                        layui.form.render("select");
                    }, error: function () {
                        alert("遍历communityList失败");
                    }
                });


                $.ajax({
                    type: "POST",
                    url: "repair/addRepair2",
                    success: function (data) {
                        $.each(data.data, function (index, item) {
                                $("#ownerList").append(new Option(item.name, item.id));
                        });
                        layui.form.render("select");
                    }, error: function () {
                        alert("遍历ownerList失败");
                    }
                });


                $.ajax({
                    type: "POST",
                    url: "repair/addRepair3",
                    success: function (data) {
                        $.each(data.data, function (index, item) {
                            $("#equipmentList").append(new Option(item.name, item.id));
                        });
                        layui.form.render("select");
                    }, error: function () {
                        alert("遍历equipmentList失败");
                    }
                });


                // // 回显
                // $.ajax({
                //
                //     type:"POST",
                //     url:"repair/updateRepair?" + "id=" +data1.id,
                //
                //     success:function(date1){
                //
                //         // $("input[name=communityId]").val([date1.community.name]);
                //         // $("input[name=ownerId]").val([date1.owner.name]);
                //         // $("input[name=equipmentId]").val([date1.equipment.name]);
                //         // $("input[name=state]").val([date1.state]);//回显
                //         layui.form.render("select");
                //     }, error: function () {
                //         alert("根据id查询失败");
                //     }
                //
                // });


                // $('[name=state]').each(function(i,item){
                //     if($(item).val()==res.state){
                //         $(item).prop('checked',true);
                //         layui.use('form',function(){
                //             var form = layui.form();
                //             form.render();
                //             alert(form);
                //         });
                //
                //     }
                // });



            });
        }

        });
</script>


<script>
    layui.use(['form', 'layedit'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        // form.verify({
        //     weeks: function (value) {
        //         if (value.length < 1) {
        //             return '课程周期至少大于1周';
        //         }
        //     },
		// 	name: function (value) {
		// 	    if (value.length < 3) {
		// 	        return '学科的名称至少得3个字符';
		// 	    }
		// 	}
        // });
        //监听提交
        form.on('submit(demo1)', function (data) {

			//请求添加接口
			$.post("repair/saveOrUpdateRepair",data.field,function(res){
				if(res.code==10000){
					//添加成功
					window.parent.location.href="listrepair.html";
				}else{
					//添加失败
					layer.alert("新增失败", {
                    title: '亲，新增失败，请检查数据'
                    })
				}

			})
            return false;
        });
    });
</script>

</body>
</html>